package com.ruoyi.web.tdmdata.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.tdmdata.domain.TdmYaoDianShouLu;
import com.ruoyi.web.tdmdata.service.ITdmYaoDianShouLuService;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * ID-04 -21 -31 药典收录Controller
 * 
 * @author ly
 * @date 2022-03-14
 */
@RestController
@RequestMapping("/tdmdata/TdmYaoDianShouLu")
public class TdmYaoDianShouLuController extends BaseController
{
    @Autowired
    private ITdmYaoDianShouLuService tdmYaoDianShouLuService;
    
    private TokenService tokenService;
    

    /**
     * 查询ID-04 -21 -31 药典收录列表
     */
    // @PreAuthorize("@ss.hasPermi('tdmdata:TdmYaoDianShouLu:list')")
    @GetMapping("/list")
    public TableDataInfo list(TdmYaoDianShouLu tdmYaoDianShouLu)
    {
        startPage();
        List<TdmYaoDianShouLu> list = tdmYaoDianShouLuService.selectTdmYaoDianShouLuList(tdmYaoDianShouLu);
        return getDataTable(list);
    }

    /**
     * 导出ID-04 -21 -31 药典收录列表
     */
    // @PreAuthorize("@ss.hasPermi('tdmdata:TdmYaoDianShouLu:export')")
    @Log(title = "ID-04 -21 -31 药典收录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TdmYaoDianShouLu tdmYaoDianShouLu)
    {
        List<TdmYaoDianShouLu> list = tdmYaoDianShouLuService.selectTdmYaoDianShouLuList(tdmYaoDianShouLu);
        ExcelUtil<TdmYaoDianShouLu> util = new ExcelUtil<TdmYaoDianShouLu>(TdmYaoDianShouLu.class);
        util.exportExcel(response, list, "ID-04 -21 -31 药典收录数据");
    }

    /**
     * 获取ID-04 -21 -31 药典收录详细信息
     */
    //@PreAuthorize("@ss.hasPermi('tdmdata:TdmYaoDianShouLu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tdmYaoDianShouLuService.selectTdmYaoDianShouLuById(id));
    }

    /**
     * 新增ID-04 -21 -31 药典收录
     */
    // @PreAuthorize("@ss.hasPermi('tdmdata:TdmYaoDianShouLu:add')")
    @Log(title = "ID-04 -21 -31 药典收录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TdmYaoDianShouLu tdmYaoDianShouLu)
    {
        return toAjax(tdmYaoDianShouLuService.insertTdmYaoDianShouLu(tdmYaoDianShouLu));
    }

    /**
     * 修改ID-04 -21 -31 药典收录
     */
    //@PreAuthorize("@ss.hasPermi('tdmdata:TdmYaoDianShouLu:edit')")
    @Log(title = "ID-04 -21 -31 药典收录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TdmYaoDianShouLu tdmYaoDianShouLu)
    {
        return toAjax(tdmYaoDianShouLuService.updateTdmYaoDianShouLu(tdmYaoDianShouLu));
    }

    /**
     * 删除ID-04 -21 -31 药典收录
     */
    //@PreAuthorize("@ss.hasPermi('tdmdata:TdmYaoDianShouLu:remove')")
    @Log(title = "ID-04 -21 -31 药典收录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tdmYaoDianShouLuService.deleteTdmYaoDianShouLuByIds(ids));
    }
    
    /**
     * 导入ID-04 -21 -31 药典收录导入
     */
    @Log(title = "药典导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
    	ExcelUtil<TdmYaoDianShouLu> util = new ExcelUtil<TdmYaoDianShouLu>(TdmYaoDianShouLu.class);
    	List<TdmYaoDianShouLu> dataList = util.importExcel(file.getInputStream());
    	System.out.println(dataList);
    	//LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
    	String operName = "ces";
    	String message = tdmYaoDianShouLuService.importUser(dataList, updateSupport, operName);
    	return AjaxResult.success(message);
    }

    /**
     * 药典导入下载模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
    	ExcelUtil<TdmYaoDianShouLu> util = new ExcelUtil<TdmYaoDianShouLu>(TdmYaoDianShouLu.class);
    	util.importTemplateExcel(response,"药典导入");
    }
}
